package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.device.DeviceBindDto;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.newBody.BindCsunNewBodyLocationDto;
import com.csun.cmny.provider.model.dto.newBody.CsunNewBodyQueryDto;
import com.csun.cmny.provider.model.enums.device.DeviceStatusEnum;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.RoomBuildingVo;
import com.csun.cmny.provider.model.vo.newBody.CsunNewBodyVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunNewBodyLocationService;
import com.csun.cmny.provider.service.DmcDeviceOnlineRecordService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.utils.DeviceUtils;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-12-27 15:05
 * @Description:
 */
@RestController
@RequestMapping(value = "/newBody", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcCsunNewBodyController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcCsunNewBodyLocationService dmcCsunNewBodyLocationService;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private DmcDeviceOnlineRecordService dmcDeviceOnlineRecordService;
    @Resource
    private DeviceUtils deviceUtils;

    /**
     * 查询日创新人感
     *
     * @param csunNewBodyQueryDto    the csun humidistat query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody CsunNewBodyQueryDto csunNewBodyQueryDto) {

        csunNewBodyQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        csunNewBodyQueryDto.setDeviceType(DmcDeviceTypeEnum.NEWBODY.getIndex());

        List<CsunNewBodyVo> csunNewBodyVos = dmcCsunNewBodyLocationService.queryListWithPage(csunNewBodyQueryDto);

        for (CsunNewBodyVo chv : csunNewBodyVos) {
            if (PublicUtil.isNotEmpty(chv.getBindType())) {
                // 设备绑定
                chv.setBind(true);
                DeviceBindDto deviceBindDto = new DeviceBindDto();
                BeanUtils.copyProperties(chv, deviceBindDto);
                RoomBuildingVo rbv = deviceUtils.getRoomBuildingVo(deviceBindDto);

                if (PublicUtil.isNotEmpty(rbv) && PublicUtil.isNotEmpty(rbv.getLocation())) {
                    chv.setLocation(rbv.getLocation());
                    chv.setBuildingName(rbv.getBuildingName());
                    chv.setFloorNo(rbv.getFloorNo());
                    chv.setRoomNo(rbv.getRoomNo());
                    chv.setOldmanId(rbv.getOldmanId());
                    chv.setOldmanName(rbv.getOldmanName());
                }
            }

            // 判断设备是否在线
            if (redisTemplate.hasKey(RedisKeyUtil.getCsunBodySensorOnline(chv.getDeviceNo()))) {
                chv.setStatus(DeviceStatusEnum.ONLINE.getKey());
            } else {
                chv.setStatus(DeviceStatusEnum.OFFLINE.getKey());
            }
        }

      //  return WrapMapper.ok(new PageInfo<>(csunNewBodyVos));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),new PageInfo<>(csunNewBodyVos));
    }

    /**
     * 查询日创新人感
     *
     * @param deviceId    the csun humidistat query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/get/{deviceId}", method = RequestMethod.POST)
    public Wrapper get(@PathVariable Long deviceId) {

        CsunNewBodyVo newBodyVo = dmcCsunNewBodyLocationService.get(deviceId);

            if (PublicUtil.isNotEmpty(newBodyVo.getBindType())) {
                // 设备绑定
                newBodyVo.setBind(true);
                DeviceBindDto deviceBindDto = new DeviceBindDto();
                BeanUtils.copyProperties(newBodyVo, deviceBindDto);
                RoomBuildingVo rbv = deviceUtils.getRoomBuildingVo(deviceBindDto);

                if (PublicUtil.isNotEmpty(rbv) && PublicUtil.isNotEmpty(rbv.getLocation())) {
                    newBodyVo.setLocation(rbv.getLocation());
                    newBodyVo.setBuildingName(rbv.getBuildingName());
                    newBodyVo.setFloorNo(rbv.getFloorNo());
                    newBodyVo.setRoomNo(rbv.getRoomNo());
                    newBodyVo.setOldmanId(rbv.getOldmanId());
                    newBodyVo.setOldmanName(rbv.getOldmanName());
                }
            }

            // 判断设备是否在线
            if (redisTemplate.hasKey(RedisKeyUtil.getCsunBodySensorOnline(newBodyVo.getDeviceNo()))) {
                newBodyVo.setStatus(DeviceStatusEnum.ONLINE.getKey());
            } else {
                newBodyVo.setStatus(DeviceStatusEnum.OFFLINE.getKey());
            }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),newBodyVo);
    }

    /**
     * 添加日创新人感
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper add(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto,
                       BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.NEWBODY.getIndex());

        Integer result = dmcDeviceService.addDevice(dmcDevice, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

      //  return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);

    }

    /**
     * 修改日创新人感
     *
     * @param dmcEditDeviceDto  the dmc edit device dto
     * @param br                the binding result
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper edit(@Validated(value = {DmcDeviceEditValidator.class})
                        @RequestBody DmcEditDeviceDto dmcEditDeviceDto,
                        BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.NEWBODY.getIndex());

        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据Id删除日创新人感
     *
     * @param id    the id
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteDmcCsunCameraById(@PathVariable Long id) {

        Integer result = dmcCsunNewBodyLocationService.deleteById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        // 删除在离线记录
        dmcDeviceOnlineRecordService.deleteByDeviceId(id);

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 绑定日创新人感
     *
     * @param bindCsunNewBodyLocationDto the bind csun humidistat location dto
     * @param br    the binding result
     *
     *
     * @return  the wrapper
     */
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Wrapper bind(@Validated @RequestBody BindCsunNewBodyLocationDto
                                bindCsunNewBodyLocationDto, BindingResult br) {
        Integer result = dmcCsunNewBodyLocationService
                .bind(bindCsunNewBodyLocationDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 解绑日创新人感
     *
     * @param deviceId  the device id
     *
     * @return the wrapper.
     */
    @RequestMapping(value = "/untied/{deviceId}", method = RequestMethod.POST)
    public Wrapper untied(@PathVariable Long deviceId) {

        Integer result = dmcCsunNewBodyLocationService.untied(deviceId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

}
